package com.sfh.dog.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import cn.bmob.v3.BmobUser;

import com.sfh.dog.R;
import com.sfh.dog.adapter.LeftMenuAdapter;
import com.sfh.dog.adapter.MainViewPageAdapter;
import com.sfh.dog.entity.LeftMenuEntity;
import com.sfh.dog.entity.User;
import com.sfh.dog.ui.base.BaseActivity;
import com.sfh.dog.util.StringUtils;

/**
 * 主页
 */
public class DogMainActivity extends BaseActivity
{
    
    private ListView lvLeftMenu;
    
    private ViewPager viewPager;
    
    private MainViewPageAdapter mainViewPageAdapter;
    
    private TextView tvTitle;
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        find();
        initSlidingMenu();
        init();
    }
    
    private void find()
    {
        lvLeftMenu = (ListView)findViewById(R.id.lvLeftMenu);
        viewPager = (ViewPager)findViewById(R.id.vpContent);
        tvTitle = (TextView)findViewById(R.id.tvTitle);
        findViewById(R.id.ivMenu).setOnClickListener(new View.OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                lvLeftMenu.setVisibility(lvLeftMenu.isShown() ? View.GONE : View.VISIBLE);
            }
        });
    }
    
    private void initSlidingMenu()
    {
        List<LeftMenuEntity> mList = new ArrayList<LeftMenuEntity>();
        mList.add(new LeftMenuEntity(R.drawable.pindao_huijiaka, "主页", true));
        mList.add(new LeftMenuEntity(R.drawable.pindao_story, "记录", true));
        mList.add(new LeftMenuEntity(R.drawable.pindao_pk, "热门", true));
        mList.add(new LeftMenuEntity(R.drawable.pindao_mcb, "达宠", true));
        mList.add(new LeftMenuEntity(R.drawable.pindao_task, "设置", true));
        mList.add(new LeftMenuEntity(R.drawable.pindao_duihuan, "发布", true));
        
        lvLeftMenu.setAdapter(new LeftMenuAdapter(this, mList));
        lvLeftMenu.setOnItemClickListener(new OnItemClickListener()
        {
            
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                switch (position)
                {
                    case 0:
                        tvTitle.setText("主页");
                        break;
                    
                    case 1:
                        tvTitle.setText("记录");
                        break;
                    
                    case 2:
                        tvTitle.setText("热门");
                        break;
                    
                    case 3:
                        tvTitle.setText("达宠");
                        break;
                    case 4:
                        
                        break;
                    case 5:
                        User bmobUser = BmobUser.getCurrentUser(DogMainActivity.this,User.class);
                        if (bmobUser != null)
                        {
                            if (StringUtils.isNullOrEmpty(bmobUser.getDogID()))
                            {
                                //   添加宠物
                                Intent intent = new Intent(DogMainActivity.this, AddDogActivity.class);
                                startActivity(intent);
                            }else
                            {
                                //   发布
                                Intent intent = new Intent(DogMainActivity.this, AddDatilActivity.class);
                                startActivity(intent);
                            }
                            
                        }
                        else
                        {
                            // 注册登录
                            Intent intent = new Intent(DogMainActivity.this, UserRegActivity.class);
                            startActivity(intent);
                        }
                        
                        break;
                    
                    default:
                        break;
                }
                
            }
        });
        
    }
    
    private void init()
    {
        viewPager.setOffscreenPageLimit(4);
        mainViewPageAdapter = new MainViewPageAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mainViewPageAdapter);
        viewPager.setOnPageChangeListener(new OnPageChangeListener()
        {
            
            @Override
            public void onPageSelected(int position)
            {
                switch (position)
                {
                    case 0:
                        tvTitle.setText("主页");
                        break;
                    
                    case 1:
                        tvTitle.setText("记录");
                        break;
                    
                    case 2:
                        tvTitle.setText("热门");
                        break;
                    
                    case 3:
                        tvTitle.setText("达宠");
                        break;
                }
                
            }
            
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2)
            {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void onPageScrollStateChanged(int arg0)
            {
                // TODO Auto-generated method stub
                
            }
        });
    }
    
}
